﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Telerik.WinControls.UI; 
using Telerik.WinControls;

using LIM.model;

namespace LIM.forms.uc_SYS_PhanQuyenHeThong
{
    public partial class uc_SYS_PhanQuyenHeThong_Menu : UserControl
    {
        radForm_SYS_PhanQuyenHeThong parentForm = null;

        public uc_SYS_PhanQuyenHeThong_Menu()
        {
            InitializeComponent();

            SetConditionsFormat();
        }

        void parentForm_OnIndexChanged()
        {
            if (parentForm.SelectedEntity != null)
            {
                BindControls();
            }
        }

        private void uc_SYS_PhanQuyenHeThong_Menu_Load(object sender, EventArgs e)
        {
            parentForm = (radForm_SYS_PhanQuyenHeThong)this.ParentForm;
            if (parentForm != null)
            {
                parentForm.OnIndexChanged +=new radForm_SYS_PhanQuyenHeThong.ListControlOnIndexChanged(parentForm_OnIndexChanged);
            }

            if (parentForm.SelectedEntity != null)
            {
                BindControls();
            }
        }

        protected void BindControls()
        {
            List<MenuPermissionsInfo> list = LIMDatas.CreateDatas_MenuPermissionsInfo();
            this.radGridView_DanhSachMenu.DataSource = list;
            
            List<tbl_SYS_MenuPermissions> menuPermissions = parentForm._SYSController.GetList_MenuPermissions_ByRowIDRole(parentForm.SelectedEntity.RowID);

            foreach (var item in this.radGridView_DanhSachMenu.Rows)
            {                
                MenuPermissionsInfo menuPermissionsInfo = (MenuPermissionsInfo)item.DataBoundItem;
                if (menuPermissions.Select(m => m.MenuName).Contains(menuPermissionsInfo.Name.ToString()))
                    item.Cells[2].Value=true;
            }


        }

        private void SetConditionsFormat()         
        {
            //add a couple of sample formatting objects             
            ConditionalFormattingObject c1 = new ConditionalFormattingObject("Applied to parent rows", ConditionTypes.DoesNotContain, "-", "", true);
            c1.RowBackColor = Color.LightBlue;
            //c1.CellBackColor = Color.FromArgb(255, 209, 140);             
            
            //c1.RowForeColor = Color.Black;             
            radGridView_DanhSachMenu.Columns[1].ConditionalFormattingObjectList.Add(c1);

            ConditionalFormattingObject c2 = new ConditionalFormattingObject("Applied to 'Start Menu' rows", ConditionTypes.StartsWith, "Start Menu:", "", true);
            c2.RowBackColor = Color.LightGray;
            radGridView_DanhSachMenu.Columns[1].ConditionalFormattingObjectList.Add(c2);
        }

        private void radMenuItem_LuuLai_Click(object sender, EventArgs e)
        {
            this.radGridView_DanhSachMenu.EndEdit();
            parentForm._SYSController.Update_MenuPermissions_ByRowIDRole(parentForm.SelectedEntity.RowID, (List<MenuPermissionsInfo>)this.radGridView_DanhSachMenu.DataSource);
            RadDesktopAlert_SavingStatus.ShowAlertSaveSuccess("Phân quyền truy cập Menu", "Đã lưu thành công!");
        }

        private void radMenuItem_Refresh_Click(object sender, EventArgs e)
        {
            if (parentForm.SelectedEntity != null)
            {
                BindControls();
            }
        } 
    }
}
